<template>
  <div>
    <el-row>
      <el-dialog v-dialogDrag :visible.sync="visible" :title="title" center :width="width">
        <el-form
          :model="form"
          size="mini"
          :rules="rules"
          ref="ruleForm"
          label-width="80px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="roleUuid">
            <el-select v-model="form.roleUuid" :placeholder="rules.roleUuid[0].message">
              <el-option v-for="(item,index) in roleList" :key="index" :label="item.roleName" :value="item.uuid">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span class="dialog-footer" slot="footer" style="text-align: right!important;">
          <el-button size="mini" @click="visible = false">取 消</el-button>
          <el-button size="mini" @click="save" type="primary">保 存</el-button>
        </span>
      </el-dialog>
    </el-row>
  </div>
</template>

<script>
  import {USER} from "@/api"
  export default {
    components: {},
    props: {
      show: {Boolean, default: false},
      value: {Object, default: {}},
      width: {String, default: "50%"},
      title: {String, default: '绑定角色'},
      roleList: {Array, default: []}
    },
    data() {
      return {
        form: {},
        rules: {
          roleUuid: [{required: true, message: "请选择角色", trigger: "blur"}]
        }
      }
    },
    computed: {
      visible: {
        get() {
          return this.show
        },
        set(val) {
          this.$emit("update:show", false)
        }
      }
    },
    mounted() {
      this.setOldData(this.value)
    },
    watch: {},
    methods: {
      setOldData(data) {
        this.form = this.$newObject(data)
      },
      save() {
        if (this.$validate(this, "ruleForm")) {
          USER.update(this.form).then(res => {
            if (res.code === 200) {
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message)
            }
            this.$emit("update:show", false)
            this.$emit('refresh')
          })
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .small-card {
    ::v-deep .el-card__body {
      padding: 10px;
    }
  }

  .el-form {
    ::v-deep .el-form-item {
      margin-bottom: 10px;
    }
  }
</style>
